package com.waya.web.browser.webview;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.waya.web.browser.common.show.FL;
import com.waya.web.browser.common.util.ApkUtil;
import com.waya.web.browser.player.PlayerProxy;
import com.waya.web.browser.player.view.MediaPlayerLayout;
import com.waye.wanba.utils.APPLog;
import com.waye.wanba.utils.ToastUtil;

/**
 * Created by JesseHev on 2018/1/16.
 * describe: 申明JS调用的Native Player的接口
 */

public class AndroidMediaplayer {

    private Context mContext;

    private PlayerProxy proxy;

    private Handler mHandler;

    private WebView mWebview;

    public AndroidMediaplayer(Context context, WebView webView, MediaPlayerLayout layout) {
        mContext = context;
        proxy = PlayerProxy.getInstance(layout);
        mHandler = new Handler(Looper.getMainLooper());
        mWebview = webView;
    }

    @JavascriptInterface
    public int setBreakpoint(int position) {
        return proxy.setBreakpoint(position);
    }

    @JavascriptInterface
    public int openDefaultPlayer(String urls) {
        return proxy.openDefaultPlayer(urls);
    }

    @JavascriptInterface
    public int setDisplay(int left, int top, int width, int height) {

        return proxy.setDisplay(left, top, width, height);
    }

    @JavascriptInterface
    public int setDataSourceList(String urls) {
        return proxy.setDataSourceList(urls);
    }

    @JavascriptInterface
    public int setDataSource(String url) {
        return proxy.setDataSource(url);
    }

    @JavascriptInterface
    public int start() {
        Toast.makeText(mContext, "start", Toast.LENGTH_SHORT).show();
        return proxy.start();
    }

    @JavascriptInterface
    public int play() {
        Toast.makeText(mContext, "play", Toast.LENGTH_SHORT).show();
        return proxy.play();
    }

    @JavascriptInterface
    public int pause() {
        Toast.makeText(mContext, "pause", Toast.LENGTH_SHORT).show();
        return proxy.play();
    }

    @JavascriptInterface
    public int getDuration() {
        Toast.makeText(mContext, "getDuration", Toast.LENGTH_SHORT).show();
        return proxy.getDuration();
    }

    @JavascriptInterface
    public int getCurrentPosition() {
        Toast.makeText(mContext, "getCurrentPosition", Toast.LENGTH_SHORT).show();
        return proxy.getCurrentPosition();
    }

    @JavascriptInterface
    public int seekTo(int position) {
        Toast.makeText(mContext, "seekTo", Toast.LENGTH_SHORT).show();
        return proxy.seekTo(position);
    }

    int result;

    @JavascriptInterface
    public int fullScreen() {
        Toast.makeText(mContext, "fullScreen", Toast.LENGTH_SHORT).show();
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                APPLog.printSimpleInfo("AbdroidPlayer js fullscreen====>" + Thread.currentThread().getName());
                result = proxy.fullScreen();
            }
        });
        return result;
    }

    @JavascriptInterface
    public int exitFullScreen() {
        Toast.makeText(mContext, "exitFullScreen", Toast.LENGTH_SHORT).show();
        return proxy.exitFullScreen();
    }

    @JavascriptInterface
    public int release() {
        Toast.makeText(mContext, "release", Toast.LENGTH_SHORT).show();
        return proxy.release();
    }

    public void onError(String code, String msg) {
        if (mWebview != null) {
            mWebview.loadUrl("javascript:onError(\"" + code + "," + msg + "\")");
        }
    }


    public void onCompleted() {
        if (mWebview != null) {
            mWebview.loadUrl("javascript:onCompleted()");
        }
    }

    public void onExit() {
        if (mWebview != null) {
            mWebview.loadUrl("javascript:onExit()");
        }
    }
}
